package rjeutsky.validators;

import rjeutsky.models.Publication;
import rjeutsky.validators.interfaces.Validator;

public class PublicationValidator implements Validator<Publication> {

	private String errorMessages;
	
	@Override
	public boolean validate(Publication publication) {
		StringBuilder errorBuilder = new StringBuilder();
		boolean validationResult = true;
		
		if (publication.getName() == null || publication.getName().isEmpty()) {
			validationResult = false;
			errorBuilder.append("name is required");
		} 
		else {
			if (publication.getName().length() > 50) {
				validationResult = false;
				errorBuilder.append("name length must be < 50");
			}
		}
		
		if (publication.getPrice() < 0) {
			validationResult = false;
			errorBuilder.append("price must be positive");
		}
		
		if (!validationResult) {
			errorMessages = errorBuilder.toString();
		}
		
		return validationResult;
	}

	@Override
	public String getValidationErrorMessage() {
		return errorMessages;
	}
	
}